package danny.hoi.web.controller.system;


import com.github.pagehelper.PageInfo;
import danny.hoi.domain.export.admin.Module;
import danny.hoi.domain.export.admin.ModuleExample;
import danny.hoi.domain.export.system.Role;
import danny.hoi.domain.export.system.RoleExample;
import danny.hoi.service.export.admin.ModuleService;
import danny.hoi.service.export.system.RoleService;
import danny.hoi.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;

    /**
     * 1.分页显示信息
     * 2.请求地址:role/list.do
     * 3.响应地址:WEB-INF/pages/system/role/role-list.jsp
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       String key) {
        if (key == null) {
            key = "";
        }
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        //设置条件查询
        criteria.andNameLike("%" + key + "%");
        criteria.andCompanyIdEqualTo(getLoginCompanyId());

        PageInfo<Role> pageInfo = roleService.findByPages(example, pageNum, pageSize);

        request.setAttribute("key", key);
        request.setAttribute("pageInfo", pageInfo);
        return "system/role/role-list";
    }

    /**
     * 1.前往添加页面
     * 2.请求地址:/role/toAdd.do
     * 3.响应地址:WEB-INF/pages/role/role-add.jsp
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        List<Role> roleList = roleService.findAll(example);
        request.setAttribute("roleList", roleList);
        return "system/role/role-add";
    }

    /**
     * 1.前往修改页面
     * 2.请求地址:/role/toUpdate.do
     * 3.响应地址:WEB-INF/pages/role/role-update.jsp
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        List<Role> roleList = roleService.findAll(example);
        //去掉当前部门和该部门的子部们
        Role role = roleService.findById(id);
        request.setAttribute("role", role);
        request.setAttribute("roleList", roleList);
        return "system/role/role-update";
    }

    /**
     * 1.更新页面
     * 2.请求地址:/role/edit.do
     * 3.响应地址:redirect:/role/list.do
     */
    @RequestMapping("/edit")
    public String edit(Role role) {
        role.setCompanyId(getLoginCompanyId());
        role.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(role.getRoleId())) {
            //添加
            role.setCreateBy(getLoginUser().getUserName());
            role.setCreateTime(new Date());
            roleService.save(role);
        } else {
            //更新
            role.setUpdateBy(getLoginUser().getUserName());
            role.setUpdateTime(new Date());
            roleService.update(role);
        }

        return "redirect:/system/role/list.do";
    }

    /**
     * 1.删除
     * 2.请求地址:"/system/role/delete.do?id="+id,
     * 3.响应地址:异步响应
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, String> delete(String id) {
        HashMap<String, String> map = new HashMap<>();
        try {
            roleService.delete(id);
            map.put("msg", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "删除失败,请联系管理员");
        }
        return map;
    }

    /**
     * 1.前往角色权限页面
     * 2.请求地址：/system/role/roleModule.do
     * 3.响应地址：WEB-INF/pages/system/role/role-module.jsp
     */
    @RequestMapping("/roleModule")
    public String roleModule(String id) {
        Role role = roleService.findById(id);
        request.setAttribute("role", role);
        return "system/role/role-module";
    }

    /**
     * 1.异步请求 ZTree的数据
     * 2.请求地址：/system/role/getZtreeNodes.do"
     * 3.异步响应
     */
    @RequestMapping("/getZtreeNodes")
    @ResponseBody
    public List<Map<String, Object>> getZtreeNodes(String roleId) {
        List<Map<String, Object>> list = new ArrayList<>();
        //查询所有的权限
        ModuleExample moduleExample = new ModuleExample();
        if(getLoginUser().getDegree() == 0){
            moduleExample.createCriteria().andBelongEqualTo("0");
        }else{
            moduleExample.createCriteria().andBelongEqualTo("1");
        }
        List<Module> moduleList = moduleService.findAll(moduleExample);
        //查询当前用户拥有的权限
        List<Module> modules = moduleService.findModuleByRoleId(roleId);
        //封装ZTreejson数据
        for (Module module : moduleList) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", module.getModuleId());
            map.put("pId", module.getParentId());
            map.put("name", module.getName());
            map.put("open", true);
            if (modules.contains(module)) {
                map.put("checked", true);
            }
            list.add(map);
        }
        return list;
    }

    /**
     * 1.修改角色权限
     * 2.请求地址：/system/role/updateRoleModule.do
     * 3.响应地址： return "redirect:/system/role/list.do";
     */
    @RequestMapping("/updateRoleModule")
    public String updateRoleModule(String roleId, String moduleIds) {
        moduleService.updateRoleModule(roleId, moduleIds);
        return "redirect:/system/role/list.do";
    }
}
